#include "Engine/Engine.h"

#include "Framework/StableHeaders.h"

#include "IO/PCFileSystem.h"
#include "Graphics/OpenGL/GLRenderEngine.h"
#include "Graphics/MaterialManager.h"
#include "Scene/ISceneManager.h"

namespace tyro
{

	

	namespace Engine
	{


		Engine::Engine( HINSTANCE windowInstance, HWND windowHandle ) 
			: windowInstance( windowInstance )
			, windowHandle ( windowHandle )
			, timer			( NULL )
			, runLog		( NULL )
			, fileSystem	( NULL )
		{
		}

		


		Graphics::IRenderWindow* Engine::Initialise()
		{
			/* Initilise file system */
			fileSystem = new IO::PCFileSystem();

			/* Delete any existing Run Log */
			if(fileSystem->Exists("Run.log"))
			{
				fileSystem->Delete("Run.log");
			}

			/* Initialise Run Log */
			runLog = new Framework::LogFile("Run.log", *fileSystem);

			/* Initialise Timer */
			timer = new Framework::Timer();

			runLog->PrintLineSeperator();
			runLog->Print( "Initializing Engine\r\n" );

			materialManager = new Graphics::MaterialManager();
			materialManager->Initialise();

			LoadConfig();

			renderEngine = new Graphics::GLRenderEngine();
			renderEngine->SetRunLog(runLog);
			renderWindow = renderEngine->Initialise();

			
			return renderWindow;


		}

		

		bool Engine::Update(FLOAT_32 timestep)
		{
			sceneManager->UpdateSceneGraph();
			renderEngine->UpdateRenderTarget();

			OS::WindowEventUtils::MessageHandler();
			return true;
		}

		bool Engine::LoadConfig()
		{
			runLog->PrintLineSeperator();
			runLog->Print("Loading Configuration File\r\n");



			return true;
		}

		FLOAT_32 Engine::GetTimeStep()
		{
			/* Get the current timestep */
			timestep = timer->GetTimeStep();
			return timestep;
		}

		Scene::ISceneManager* Engine::CreateSceneManager()
		{
			sceneManager = new Scene::ISceneManager();
			sceneManager->SetActiveRenderEngine(renderEngine);
			sceneManager->Init();
			return sceneManager;
		}

		Graphics::IRenderEngine* Engine::GetRenderEngine()
		{
			return renderEngine;
		}

		Graphics::MaterialManager* Engine::GetMaterialManager()
		{
			return materialManager;
		}

	}
}